home *** CD-ROM | disk | FTP | other *** search
- Path: gollum.kingston.net!usenet
- From: girard@haventree.com (Eugene Girard)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP with a simple C Structure
- Date: Wed, 03 Apr 1996 20:33:12 GMT
- Organization: HavenTree Software, Limited
- Message-ID: <4juqqo$j99@gollum.kingston.net>
- References: <4jm38u$j1a@news.bellglobal.com>
- NNTP-Posting-Host: buddy.haventree.com
- X-Newsreader: Forte Free Agent v0.55
-
- bogdanfl@aei.ca (Bogdan Florescu) wrote:
-
- (edited slightly for brevity...)
-
- > Employee Jim, Paul, Pat, Scott;
-
- > Jim.department='a';
- > Paul.department='b';
- > ................................
-
- >I need to replace the dots with a program that tells me who else works
- >in Paul's department and how much money he makes.
-
- >I would like to have the structure variables named with the name of
- >the employee, (instead of an array with the name of the employee as a
- >member) so I can easily access their members. The structure can be
- >modified, but I really need to be able to have expressions such as
- >Jim.salary.
-
- There is a conceptual problem here. Basically, the name of the
- variable "Jim" is not directly visible at run-time. The compiler
- basically replaces "Jim.whatever" with an actual address in memory,
- and thereafter forgets what the developer called the variable. But
- this doesn't even come close to answering your original question...
-
- What you might want to do is to build up some association between the
- variables and the employees names. You might want to try something
- like:
-
- struct lookupTable{
- char *employeeName;
- Employee *data;
- } **table = 0;
- int tableSize = 0;
-
- void RegisterEmployee( char *name, Employee *data)
- {
- table = (struct lookupTable **) realloc(
- table, sizeof( struct lookupTable*) * tableSize);
- table[ tableSize] = (struct lookupTable *) malloc(
- sizeof( struct lookupTable));
- table[ tableSize].employeeName = strdup( name);
- table[ tableSize++].data = data; /* copy of real data. We don't own
- this! */
- }
-
- Then you can add code like:
- RegisterEmployee( "John", &John);
- in your program, and write a fairly straightforward query to find the
- information that you originally requested.
-
- >Thank you,
- >Bogdan Florescu
- S'Alright.
- Gene
- --
- Eugene Girard, Programmer, HavenTree Software Limited
- HavenTree makes EasyFlow (Windows, DOS, MAC) and Nodemap (Windows, DOS)
- For more information, check out http://www.haventree.com
-
-